﻿using System;
using System.Data;
using System.Linq;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {

        private void JLTAB45DOC(DataSet db)
        {
            string value = "";
            DataRow dwABPOS = null;
            int rStart = 0;
            int nRow = 1;
            int nPRow = 1;
            int idx = 0;
            int pages = 0;
            dwABPOS = db.Tables["ABPOS"].Rows[0];
            DataRow dwABK = db.Tables["ABKOPF"].Rows[0];
            DataRow dwWS = db.Tables["WASTAMM"].Rows[0];
            string art = dwABPOS["ABPART"].ToString();
            string var = dwABPOS["ABPVAR"].ToString();
            string color = dwABPOS["ABPFARBE"].ToString();
            #region====Header======
            //exAgent.SetCellValue(1, 23, "*" + dwABPOS["ABPAUFKRS"].ToString() + dwABPOS["ABPAUFLFD"].ToString() + dwABPOS["ABPAUFPOS"].ToString() + "*");//barcode

            exAgent.SetCellValue(3, 5, dwABPOS["ABPAUFKRS"].ToString() + "-" + Helpers.Helper.GetSubRollNum(dwABPOS["ABPAUFLFD"].ToString())
                + "-" + Helpers.Helper.GetSubRollNum(dwABPOS["ABPAUFPOS"].ToString()));
            string CO="";
            CO=dwABK["ABKKDAUFTNR"].ToString().Trim();
            if(CO=="")
            {
                CO = dwABPOS["ABPART"].ToString().Trim();
            }
            exAgent.SetCellValue(3, 15, CO);
            exAgent.SetCellValue(3, 23, dwABPOS["ABPFARBE"].ToString().Trim());
            string PRDATE = DateTime.Now.Year.ToString() +"-"+ DateTime.Now.Month.ToString()+"-"+ DateTime.Now.Day.ToString(); ;
            exAgent.SetCellValue(3, 29, PRDATE);
            exAgent.SetCellValue(4, 5, GetGB2312Value(dwWS["WABEZ"].ToString()));
            exAgent.SetCellValue(5, 5, Convert.ToDouble(dwABPOS["ABPSOLLMENGE1"].ToString()) / 100);
            exAgent.SetCellValue(5, 15, Convert.ToDouble(dwABPOS["ABPSOLLMENGE8"].ToString()) / 100);
            string STARTDATE = dwABPOS["ABPSTARTDAT"].ToString().Substring(0,4) + "-" + dwABPOS["ABPSTARTDAT"].ToString().Substring(4, 2)
                + "-" + dwABPOS["ABPSTARTDAT"].ToString().Substring(6, 2);
            exAgent.SetCellValue(5, 23, STARTDATE);
            string ENDDATE = dwABPOS["ABPLIEFDAT"].ToString().Substring(0, 4) + "-" + dwABPOS["ABPLIEFDAT"].ToString().Substring(4, 2)
                + "-" + dwABPOS["ABPLIEFDAT"].ToString().Substring(6, 2);
            exAgent.SetCellValue(5, 29, ENDDATE);
            exAgent.SetCellValue(8, 5, dwABPOS["ABPFILLER1"].ToString());
            #endregion


            #region======Beam=========
            rStart = 12;
            nRow = 1;
            nPRow = 2;
            idx = 0;
            foreach (DataRow row in db.Tables["BABST"].Rows)
            {
                exAgent.SetCellValue(Convert.ToInt16(rStart + Math.Ceiling(Convert.ToDouble(idx / nPRow)) * nRow),
                    1 + pages * 18, row["BMBAUMKRS"].ToString() + "-" + Helpers.Helper.GetSubRollNum(row["BMBAUMLFD"].ToString()));
                idx++;
                pages++;
                if (pages == 2)
                {
                    pages = 0;
                }
            }
            #endregion
            #region====yarn=========
            rStart = 27;
            nRow = 2;
            nPRow = 1;
            idx = 0;
            int ycount = db.Tables["GAAUF"].Rows.Count;
            foreach (DataRow rw in db.Tables["GAAUF"].Rows)
            {
                string gaart = rw["GAART"].ToString();
                string gavar = rw["GAVAR"].ToString();
                string gacolor = rw["GAFARBE"].ToString();
                string GSCBEZ =Helpers.Helper.Convert8859P1ToGB2312(rw["GSCBEZ"].ToString());
                
                double gakg1 = System.Convert.ToDouble(rw["GAKG1"].ToString()) * 0.01;
                double gakg3 = System.Convert.ToDouble(rw["GAKG3"].ToString()) * 0.01;
                double gakg5 = System.Convert.ToDouble(rw["GAKG5"].ToString()) * 0.01;
                string yarn = gaart.Trim();
                if (gavar.Trim() != "")
                    yarn += "-" + gavar.Trim();
                if (gacolor.Trim() != "")
                {
                    if (gacolor.Trim() != "000000")
                    {
                        yarn += "-" + gacolor.Trim();
                    }
                }
                #region=====Alloction=======
                if (dsSource.Tables.Contains("ZUTEI"))
                {
                    var qzu = from rows in db.Tables["ZUTEI"].AsEnumerable()
                              where rows["ZUAUFKRS"].ToString() == rw["GAAUFKRS"].ToString()
                                 & rows["ZUAUFLFD"].ToString() == rw["GAAUFLFD"].ToString()
                                & rows["ZUAUFPOS"].ToString() == rw["GAAUFPOS"].ToString()
                                 & rows["ZUSA"].ToString() == rw["GASA"].ToString()
                                 & rows["ZUPOS"].ToString() == rw["GAPOS"].ToString()
                              select rows;
                    string filters = "";
                    DataRow[] rx;
                    DataRow dwPartie = null;
                    string info = "";
                    int zucount = qzu.Count();
                    int zupos = 0;
                    foreach (DataRow zu in qzu)
                    {
                        // ZUPARTIEZULO	ZULB ZUEINTKG ZUENTNKG
                        double allocated = System.Convert.ToDouble(zu["ZUEINTKG"].ToString().Trim()) * 0.001;
                        //double get = System.Convert.ToDouble(zu["ZUENTNKG"].ToString().Trim()) * 0.001;
                        //double TOGET = allocated - get;
                        string sppartie = zu["ZUSPPART"].ToString().Trim();
                        //exAgent.SetCellValue(rStart + idx * nRow, 27, GetGB2312Value(sppartie));
                        //exAgent.SetCellValue(rStart + idx * nRow, 22, allocated);
                    }
                }
                #endregion
                exAgent.SetCellValue(rStart + idx * nRow, 3, yarn);
                exAgent.SetCellValue(rStart + idx * nRow + 1, 3, GSCBEZ);
                exAgent.SetCellValue(rStart + idx * nRow, 19, gakg1);
                exAgent.SetCellValue(rStart + idx * nRow, 23, gakg3);
                exAgent.SetCellValue(rStart + idx * nRow, 26, gakg5);

                var query = from DataRow row in db.Tables["BASTGA"].AsEnumerable()
                            where row["BAGGART"].ToString() == gaart
                            && row["BAGGVAR"].ToString().Trim() == gavar.Trim()
                            && row["BAGGFARBE"].ToString() == gacolor
                            && row["BAGART"].ToString() == art
                            && row["BAGVAR"].ToString().Trim() == var.Trim()
                            && row["BAGFARBE"].ToString() == color
                            select row;
                foreach (DataRow row in query)
                {
                    exAgent.SetCellValue(rStart + idx * nRow, 15, row["BAGFDZ"].ToString());
                }
                idx++;
                if (idx < ycount)
                {
                    exAgent.RangeCopy(27, 28, rStart + idx * nRow, rStart + idx * nRow + 1);
                    exAgent.SetCellValue(rStart + idx * nRow, 1, idx+1);
                }
            }
            #endregion
            #region=========footer===========
            exAgent.SetCellValue(rStart + idx * nRow + 1, 1, "制表人：			_____________");
            //exAgent.SetCellValue(rStart + idx * nRow + 3 + 1, 1, "日期：			_____________");
            exAgent.SetCellValue(rStart + idx * nRow + 1, 23, "主管：			_____________");
            //exAgent.SetCellValue(rStart + idx * nRow + 3 + 1, 1, "日期：			_____________");
            //exAgent.SetCellValue(rStart + idx * nRow + 3, 23, "经理：			_____________");
            //exAgent.SetCellValue(rStart + idx * nRow + 3 + 1, 1, "日期：			_____________");
            #endregion
        }
    }
}